import { reqLogin } from "@/api";

export default {
  namespaced: true,
  state: {
    // 获取token
    token: localStorage.getItem("token"),
    userInfo: {},
  },
  mutations: {
    // 存储token
    SET_TOKEN(state, token) {
      state.token = token;
    },
    // 存储用户信息
    SET_USERINFO(state, userInfo) {
      state.userInfo = userInfo;
    },
  },
  actions: {
    // 登录
    async getLogin({ commit }, { phone, captcha }) {
      let result = await reqLogin({ phone, captcha });
      if (result.code === 200) {
        // 获取token
        const token = `MUSIC_U=${result.token}`;
        // 获取用户信息
        const profile = {
          userId: result.profile.userId,
          nickname: result.profile.nickname,
          avatarUrl: result.profile.avatarUrl,
        };
        commit("SET_TOKEN", token);
        commit("SET_USERINFO", profile);
        // 存储token
        localStorage.setItem("token", result.token);
        localStorage.setItem("profile", result.token);
        return "ok";
      } else {
        return Promise.reject("fail");
      }
    },
    // 获取用户信息
    // async getUserInfo({ commit }) {
    //   let result = await reqUserInfo();
    //   if (result.code === 200) {
    //     // console.log(result.data)
    //     commit("SET_USERINFO", result.data);
    //     return "ok";
    //   } else {
    //     return Promise.reject("fail");
    //   }
    // },
  },
  getters: {},
};
